package com.xxx.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxx.blog.entity.Books;
import com.xxx.blog.mapper.BooksMapper;
import com.xxx.blog.service.BooksService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author WangXJ
 * @since 2021-08-08
 */
@Service
public class BooksServiceImpl extends ServiceImpl<BooksMapper, Books> implements BooksService {

    @Autowired
    BooksMapper booksMapper;

    @Override
    public List showAll() {
        return booksMapper.selectList(null);
    }

    @Override
    public List findNewBook() {
        return booksMapper.findNewBook();
    }

    @Override
    public List<Books> findNewByType(int currentPage, int size,int type) {
        Page<Books> page = new Page<>(currentPage,size);
        QueryWrapper<Books> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        wrapper.eq("type",type);
        booksMapper.selectPage(page,wrapper);
        return page.getRecords();
    }

    @Override
    public Map<String,Object> findAllByType(int currentPage, int size, int type) {
        Page<Books> page = new Page<>(currentPage,size);
        QueryWrapper<Books> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        wrapper.eq("type",type);
        booksMapper.selectPage(page,wrapper);
        Map<String,Object> map = new HashMap<>();
//        map.put("totalPage",page.getPages());
//        map.put("records",page.getRecords());
        map.put("page",page);

        return map;
    }

    @Override
    public Books findBookById(int bookId) {
        return booksMapper.selectById(bookId);
    }

    @Override
    public List<Books> findBookByName(String bName) {
        QueryWrapper<Books> wrapper = new QueryWrapper<>();
        wrapper.like("bName",bName);
        return booksMapper.selectList(wrapper);
    }

}
